#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/4/14 19:06
# @Author  : Grayson Liu
# @Email   : graysonliu@foxmail.com
# @File    : GraphicOutputWizardDialog.py
from PyQt5.QtWidgets import QDialog, QFileDialog

from ui.Ui_GraphicOutputWizardDialog import Ui_GraphicOutputWizardDialog
from CustomSignal import CustomSignal


class GraphicOutputWizardDialog(QDialog, Ui_GraphicOutputWizardDialog):
    """
    设置遥感影像输出的对话框
    """
    global_signal = CustomSignal()
    output_type_dic = {
        ".jpg": 0,
        ".tiff": 1
    }

    def __init__(self, output_dic: dict, parent=None):
        """
        初始化
        :param output_dic: 从ProjectForm传递而来的输出信息
        :param parent: 父窗体
        """
        super(GraphicOutputWizardDialog, self).__init__(parent)
        self.setupUi(self)

        # 将ProjectForm传递而来的输出信息显示在当前界面上
        if output_dic:
            self.txtOutputPath.setText(output_dic["path"])
            self.comboBoxType.setCurrentIndex(GraphicOutputWizardDialog.output_type_dic[output_dic["type"]])

        # 绑定信号和槽
        self.btnCancel.clicked.connect(lambda: self.close())
        self.btnConfirm.clicked.connect(self.confirm_output)
        self.btnBrowse.clicked.connect(self.browse_file)

    def browse_file(self):
        """
        对话框”浏览“按钮的槽函数
        :return:
        """
        # 打开文件浏览器，选择输出图像的保存位置
        output_path, output_type = QFileDialog.getSaveFileName(self, "选择输出图像保存位置",
                                                               filter="GeoTiff Files(*.tif);;JPEG Files(*.jpg);;"
                                                                      "All Files(*.*)")
        if output_path == "":
            return
        else:
            self.txtOutputPath.setText(output_path)

    def confirm_output(self):
        """
        对话框“确认”按钮的槽函数
        :return:
        """
        output_dic = {
            "path": self.txtOutputPath.text(),
            "type": self.comboBoxType.currentText(),
        }
        # 给主窗体发信号，把信息发送给ProjectForm进行处理
        GraphicOutputWizardDialog.global_signal.set_output.emit(output_dic)
        self.close()
